package com.jrx.anytxn.transaction.service.impl;

import com.alibaba.fastjson.JSON;
import com.jrx.anytxn.common.constant.Constants;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.customer.dto.coupon.CouponReq;
import com.jrx.anytxn.customer.dto.customer.CustomerRes;
import com.jrx.anytxn.customer.service.ICouponInputService;
import com.jrx.anytxn.customer.service.ICustomerService;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.AmLoanMainInfo;
import com.jrx.anytxn.transaction.service.IActivityLoanService;
import com.jrx.anytxn.transaction.service.ILoanMainInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;

/**
 * ClassName ActivityLoanServiceImpl
 *
 * @author lujianyou
 * @version 1.0
 * @description 根据借据派生的优惠券活动服务 实现
 * @date 2021/7/9 9:51
 **/
@Service
public class ActivityLoanServiceImpl implements IActivityLoanService {
    private static final Logger logger = LoggerFactory.getLogger(ActivityLoanServiceImpl.class);

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ILoanMainInfoService loanMainInfoService;
    @Autowired
    private ICouponInputService couponInputService;

    /**
     * 统计消费金额，满足某域值发放特定优惠券
     *
     * @param couponReq
     * @param expenditureThre 活动金额 域值
     */
    @Override
    public void sendCouponByLoanStatMonthly(CouponReq couponReq, BigDecimal expenditureThre) throws TxnException {

        CustomerRes customer = customerService.getCustomerByChannelAndUserId(couponReq.getChannel(),
                couponReq.getUserId(), couponReq.getTenantId());
        BigDecimal loanSum = loanMainInfoService.sumByCustomerIdAndLoanTypeMonthly(
                customer.getSecondLevel().getCustomerId(), TransBizConstant.LOAN_TYPE_4, couponReq.getTenantId(), Constants.DEFAULT_PRODTCT_CODE);
        if (logger.isInfoEnabled()) {
            logger.info("sendCouponByLoanStat couponReq参数:{},活动域值expenditureThre:{}, 当月消费总额loanSum:{}",
                    JSON.toJSONString(couponReq), expenditureThre, loanSum);
        }
        // 如果消费金额
        if (!Objects.isNull(loanSum) && expenditureThre.compareTo(loanSum) <= 0) {
            couponInputService.grantCoupon(couponReq);
        } else {
            logger.info("月累计消费:{}不满：{}", loanSum, expenditureThre.intValue());
        }
    }

    /**
     * 半价过周末统计值
     *
     * @param tenantId
     * @param sumAmount
     * @return
     */
    @Override
    public Integer halfActivityWeekend(String tenantId, BigDecimal sumAmount) {
        String monday = DateUtils.getDayOfWeek(Calendar.MONDAY) + " 00:00:00";
        String thursday = DateUtils.getDayOfWeek(Calendar.THURSDAY) + " 23:59:59";
        return loanMainInfoService.countDistinctlyGroupByCustomerId(tenantId, monday, thursday,
                TransBizConstant.LOAN_TYPE_4, sumAmount, Constants.DEFAULT_PRODTCT_CODE);
    }

    @Override
    public List<String> getAmLoanMainInfoByConsumeAmountWeekly(String startTime, String endTime, String loanType, BigDecimal sumAmount, String productId) {
        return loanMainInfoService.getAmLoanMainInfoByConsumeAmountWeekly(startTime, endTime, loanType, sumAmount, productId);
    }

}
